const Koa = require('koa');
const pug = require('pug');
const views = require('koa-views');
// const { htmTpl, pugTpl } = require('./tpl/index');
const {
    connect,
    initSchemas
} = require('./database/init');
// const router = require('./routes');
const R = require('ramda');
const {
    resolve
} = require('path');
const MIDDLEWARES = ['router', 'parcel'];

const app = new Koa();
const port = 2333;
const useMiddlewares = (app) => {
    R.map(
        R.compose(
            R.forEachObjIndexed(
                initWith => initWith(app)
            ),
            require,
            name => resolve(__dirname, `./middlewares/${name}`)
        )
    )(MIDDLEWARES)
}

;(async () => {
    await connect();

    initSchemas();
    // require('./task/movie'); // 执行子进程爬数据
    // require('./task/api'); // 执行子进程获取豆瓣api数据

    await useMiddlewares(app);

    app.listen(port, () => {
        console.log(`端口${port} 服务启动成功！`);
    });
})()

// app.use(router.routes())
//    .use(router.allowedMethods)

// app.use(async (ctx, next) => {
//     ctx.type = 'text/html; charset=utf-8';
//     // ctx.body = htmTpl;
//     ctx.body = pug.render(pugTpl, {
//         you: 'luck',
//         me: 'willerfu'
//     });
// })

// app.use(views(__dirname + '/views', {
//     extension: 'pug'
// }));
// app.use(async function (ctx) {
//     await ctx.render('index', {
//         you: 'luck',
//         me: 'willerfu'
//     });
// })